package ex3.controller.date.ccf;

import java.util.Scanner;

/**
 * @AUTHOR 尹书岿
 * @DATE 2024/12/11 20:51
 * @DESCRIBE
 */
public class moBanCreate {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        StringBuilder[] model = new StringBuilder[m];
        String[] param = new String[n];
        StringBuilder[] paramvalue1 = new StringBuilder[m];
        String[] paramvalue2 = new String[n];
        String blank = sc.nextLine().trim();

        for (int i =0; i < m; i++){
            model[i] = new StringBuilder(sc.nextLine().trim());
        }
        //变量替换
        for (int i = 0; i < n;i++){
            param[i] = sc.next();
            param[i] = "{{ " + param[i] + " }}";  //注意空格
            paramvalue1[i] = new StringBuilder(sc.nextLine().trim());
            paramvalue2[i] = paramvalue1[i].toString();
            paramvalue2[i] = paramvalue2[i].substring(1,paramvalue2[i].length()-1);
        }

        for (int i = 0; i < m;i++){
            for (int j = 0; j < n;j++) {
                model[i] = new StringBuilder(model[i].toString().replace(param[j],paramvalue2[j]));
            }
        }
        for (int i = 0; i < m;i++) {
            model[i] = new StringBuilder(model[i].toString().replaceAll("\\{\\{.+\\}\\}",""));
        }

        for (int i = 0; i < m;i++) {
            System.out.println(model[i].toString());
        }
    }
}
